//
//  XHRTopView.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/4.
//  Copyright © 2016年 xuhongru. All rights reserved.
//

#import "XHRTopView.h"
#import <UIImageView+WebCache.h>
#import "XHRWeatherItem.h"

@interface XHRTopView()
/**分割线*/
@property(nonatomic,weak)UIImageView *separateView;
/**天气图片*/
@property(nonatomic,weak)UIImageView *weatherImage;
/**天气label*/
@property(nonatomic,weak)UILabel *weatherLabel;
/**地区button*/
@property(nonatomic,weak)UIButton *localButton;
/**温度lable*/
@property(nonatomic,weak)UILabel *temperatureLabel;
/**日期label*/
@property(nonatomic,weak)UILabel *dateLabel;

@property (nonatomic,copy) NSString *location;
@end

@implementation XHRTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        

    }
    return self;
}
- (void)setupUI
{
    self.backgroundColor = XHRColorWithRGB(154, 239, 210);
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"location"]) {
        self.location = [[NSUserDefaults standardUserDefaults] objectForKey:@"location"];
    } else {
        self.location = @"上海";
    }
}
- (void)setWeatherItem:(XHRWeatherItem *)weatherItem
{
    _weatherItem = weatherItem;
    
    self.weatherLabel.text = weatherItem.weather;
 
    self.temperatureLabel.text = [NSString stringWithFormat:@"温度:%@",weatherItem.temperature];
    
    NSString *dateString = weatherItem.date;
    dateString = [dateString componentsSeparatedByString:@"("].firstObject;
    self.dateLabel.text = [NSString stringWithFormat:@"日期:%@",dateString];
    
    [self.weatherImage sd_setImageWithURL:[NSURL URLWithString:weatherItem.nightPictureUrl] placeholderImage:[UIImage imageNamed:@"good_image_default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
}
#pragma mark -
#pragma mark - layoutSubviews
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.separateView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(1);
        make.top.bottom.equalTo(0);
        make.center.equalTo(self);
    }];
    [self.weatherImage makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.offset(15);
        make.width.equalTo(35);
        make.height.equalTo(30);
    }];
    [self.weatherLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(self.weatherImage.trailing).offset(15);
        make.width.equalTo(50);
    }];
    [self.localButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self.separateView.leading).offset(-10);
    }];
    [self.temperatureLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.leading.equalTo(self.separateView.trailing).offset(0);
        make.trailing.offset(0);
    }];
    [self.dateLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-5);
        make.leading.equalTo(self.separateView.trailing).offset(0);
        make.trailing.offset(0);
    }];
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickLocalButton:(UIButton *)sender
{
    NSLog(@"%s",__func__);
    
    if ([self.delegate respondsToSelector:@selector(pushCityVc)]) {
        [self.delegate pushCityVc];
    }
}



#pragma mark
#pragma mark - 刷新地区数据
- (void)setLocalStr:(NSString *)localStr{
    _localStr = localStr;
    
    [self.localButton setTitle:self.localStr forState:UIControlStateNormal];
}




#pragma mark -
#pragma mark -  懒加载分割线
- (UIImageView *)separateView
{
    if (!_separateView) {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [self addSubview:imageView];
        
        _separateView = imageView;
    }
    return _separateView;
}
#pragma mark -
#pragma mark - 懒加载天气图片
- (UIImageView *)weatherImage
{
    if (!_weatherImage) {
        UIImageView *imageView = [[UIImageView alloc]init];
        [self addSubview:imageView];
        _weatherImage = imageView;
    }
    return _weatherImage;
}
#pragma mark -
#pragma mark - 懒加载天气label
- (UILabel *)weatherLabel
{
    if (!_weatherLabel) {
        UILabel *label = [[UILabel alloc]init];
        [self addSubview:label];
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = XHRColorWithRGB(83, 206, 192);
        _weatherLabel = label;
    }
    return _weatherLabel;
}
#pragma mark -
#pragma mark - 懒加载位置按钮
- (UIButton *)localButton
{
    if (!_localButton) {
        UIButton *button = [[UIButton alloc]init];
        [button addTarget:self action:@selector(didClickLocalButton:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:XHRColorWithRGB(83, 206, 192) forState:UIControlStateNormal];
        [button setTitle:self.location forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:button];
        _localButton = button;
        
        NSLog(@"button----->%@", button);
    }
    return _localButton;
}
#pragma mark -
#pragma mark - 懒加载温度label
- (UILabel *)temperatureLabel
{
    if (!_temperatureLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = XHRColorWithRGB(83, 206, 192);
        [self addSubview:label];
        _temperatureLabel = label;
    }
    return _temperatureLabel;
}
#pragma mark -
#pragma mark - 懒加载日期label
- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = XHRColorWithRGB(83, 206, 192);
        [self addSubview:label];
        _dateLabel = label;
    }
    return _dateLabel;
}


@end
